package logic

import (
	"context"
	"fmt"
	"user666/model/mysql"
	"user666/model/redis"

	"user666/userrpc/internal/svc"
	"user666/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UploadLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadLogic {
	return &UploadLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UploadLogic) Upload(in *userrpc.UploadRequest) (*userrpc.UploadResponse, error) {
	// todo: add your logic here and delete this line
	get := redis.Client.Get("SendSmsupload" + in.Username)
	if get.Val() != in.Code {
		return nil, fmt.Errorf("验证码错误,请重新输入")
	}
	var finduser mysql.User
	mysql.DB.Find(&finduser)
	if finduser.ID == 0 {
		return nil, fmt.Errorf("用户还没注册不能修改")
	}
	if finduser.Password != in.Password {
		return nil, fmt.Errorf("原密码输入错误")
	}
	if in.Password == in.Newpass {
		return nil, fmt.Errorf("新密码不能等于原密码")
	}
	if in.Newpass != in.Oldpass {
		return nil, fmt.Errorf("两次密码输入不一致")
	}
	finduser.Password = in.Newpass
	mysql.DB.Updates(&finduser)
	return &userrpc.UploadResponse{Id: int64(finduser.ID)}, nil
}
